package operation;

import book.Book;
import book.BookList;
import com.sun.media.sound.RIFFInvalidDataException;

import java.util.Scanner;

/**
 * @ClassDescription:
 * @JdkVersion: 1.8
 * @Author: 李白
 * @Created: 2024/7/29 21:57
 */
public class AddOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("添加书籍");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名");
        String name = scanner.nextLine();
        System.out.println("请输入作者名");
        String author = scanner.nextLine();
        System.out.println("请输入价格");
        int price = scanner.nextInt();
        scanner.nextLine();
        System.out.println("请输入类型");
        String type  = scanner.nextLine();
        Book book = new Book(name,author,price,type);
        //检查是否有重复的，有则不录入
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize ; i++) {
            Book book1 =bookList.getBook(i);//
                    if (book1.getName().equals(name)){
                        System.out.println("已有本书籍，无需添加");
                        return;
                    }
                    //检查书架是否满的
        }if (currentSize == bookList.getBooks().length){
            System.out.println("书架满了，不能添加");
            return;
        }else{
            bookList.setBooks(currentSize,book);
            bookList.setUsedSize(currentSize+1);//添加过后，当前的书架数量变化，所以需要立即更改
        }


    }
}
